---
--- Created by root.
--- DateTime: 17-11-21 下午1:57
---

local _OBJ = {}

--获取文章列表
_OBJ.articleList = function(self,search_key, page, size)
    local start = (page-1) * size
    local data = {}
    if string.len(search_key)>1 then
        data = db:query("select id,title,intro,image,class from article where title like \"%"..search_key.."%\" order by id desc limit "..start..","..size.."")
    else
        data = db:query("select id,title,intro,image,class from article order by id desc limit "..start..","..size.."")
    end
    if data then
        for k, v in pairs(data) do
            data[k]["class"] = "程序语言"
        end
        return data
    else
        return {}
    end
end

_OBJ.add = function(self,content, image, intro, title, class)
    local content = ngx.quote_sql_str(content) --防止sql注入
    local image = ngx.quote_sql_str(image) --防止sql注入
    local intro = ngx.quote_sql_str(intro) --防止sql注入
    local title = ngx.quote_sql_str(title) --防止sql注入
    local class = tonumber(class) or 1 --防止sql注入

    local sql = "insert into article(intro,content,class,title,image) values("..intro..","..content..","..class..","..title..","..image..")"
    local res, err, errcode, sqlstate = db:query(sql)
    return res
end

_OBJ.detail = function(self,aid)

    local data = db:query("select id,title,intro,image,content from article where id="..aid)
    if data[1] then
        return data[1]
    else
        return {}
    end
end


return _OBJ